<?php
class CategoriesController extends AppController {

	var $name = 'Categories';

	function beforeFilter(){
		parent::beforeFilter();
		$allowed_actions=array('view');
		if (!in_array($this->action,$allowed_actions)){
			$this->redirect(array('controller' => 'songs', 'action' => 'index'));
		}
		$this->Auth->allow('view');

	}

	function index() {
		$this->Category->recursive = 0;
		$this->set('categories', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid category', true));
			$this->redirect(array('action' => 'index'));
		}
		$category = $this->Category->read(null, $id);

		//for rss
		$channel = array('title' => 'AUDIVIT | List of '.$category['Category']['title'],
			'link' => '/categories/view/'.$category['Category']['id'],
			'description' => 'Recent song posts to '.$category['Category']['title'],
			'language' => 'en-us',
		);

		//loading scores for user
		$this->loadModel('Score');
		$conditions = array("Score.user_id" => $this->Auth->user('id'), "Song.category_id" => $id);
		$scores = $this->Score->find('all', array('conditions' => $conditions));

		$conditions = array("Song.category_id" => $id);
		App::Import('Model', 'Song'); 
		$this->Song = new Song; 
		$this->paginate = array('order' => 'Song.score/(now()- Song.date) DESC', 'limit' => '20','conditions'=> $conditions);
		$songs = $this->paginate('Song');
		foreach ($songs as $i=>$song){
			$songs[$i]['Sitemask']['embedurl'] = preg_replace('/__url__/',$song['Song']['url_for_embed'],$song['Sitemask']['embedurl']);
		}

		$this->set( 'songs', $songs); 
		$this->set('scores', $scores);
		App::Import('Model', 'Group'); 
		$this->Group = new Group; 
		$groups = $this->Group->find('all', array('order'=>array('Group.title asc')));
		$categories = $this->Song->Category->find('all',array('order'=>array('Category.title asc')));

		$this->set(compact('categories', 'groups','channel', 'category'));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Category->create();
			if ($this->Category->save($this->data)) {
				$this->Session->setFlash(__('The category has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The category could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid category', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Category->save($this->data)) {
				$this->Session->setFlash(__('The category has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The category could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Category->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for category', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Category->delete($id)) {
			$this->Session->setFlash(__('Category deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Category was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
